package login

import (
	"../../db"
	"encoding/json"
	"github.com/go-redis/redis"
	"time"
)

/*
 * 描述: 登陆二维码
 *
 *******************************************************************/

const DEF_UUID_LIVE = 20 * 90 * time.Second

type QrStatus struct {
	State  int   `json:"state"` // 2扫码成功
	UserId int64 `json:"userId"`
}

func Ur(f int) *redis.Client {
	return db.UseRedis(f)
}

type QrLogin struct {
	UUId  string // key
	Value QrStatus
}

func (this *QrLogin) RKey() string { return this.UUId }

func (this *QrLogin) RDb() int { return 0 }

func (this *QrLogin) Set(val *QrStatus) error {
	sVal, _ := json.Marshal(val)
	return Ur(this.RDb()).Set(this.RKey(), string(sVal), DEF_UUID_LIVE).Err()
}
func (this *QrLogin) Get() error {
	val, err := Ur(this.RDb()).Get(this.RKey()).Bytes()
	if err != nil {
		return err
	}
	if err := json.Unmarshal(val, &this.Value); err != nil {
		return err
	}
	return nil
}
func (this *QrLogin) Exist() (int64, error) {
	return Ur(this.RDb()).Exists(this.RKey()).Result()
}
